﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace KuPeiXun.Utility
{
    /// <summary>
    /// 图片工具
    /// </summary>
    /// ===============================================================
    /// 作者：邹学良 
    /// 时间：2013-03-21
    /// 描述：创建
    /// 历史：无
    /// ===============================================================
    public class ImageHelper
    {
        /// <summary>
        /// 按照宽度缩放
        /// </summary>
        /// <param name="imagestream">原图的流</param>
        /// <param name="height"></param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static void ThumbnailImageSize(Stream imagestream, float width, float height, string savePath, ThumbnailType type)
        {
            try
            {
                //加载原图
                Image original = Image.FromStream(imagestream);

                //原图的高度
                float currHeight = original.Height;
                //原图的宽度
                float currWidth = original.Width;
                //缩放后的宽度
                float smallWidth = 0;
                //缩放后的高度
                float smallHeight = 0;
                //判断原始高度是否大于将要缩小的宽度
                switch (type)
                {
                    case ThumbnailType.Height:
                        if (currHeight > height)
                        {
                            smallWidth = currWidth * height / currHeight;
                        }
                        if (smallWidth > width && width != 0)
                        {
                            smallWidth = width;
                        }
                        smallHeight = height;
                        break;
                    case ThumbnailType.Width:
                        if (currWidth >= width)
                        {
                            smallHeight = (currHeight * width) / currWidth;
                        }
                        else
                        {
                            smallWidth = currWidth;
                            smallHeight = currHeight;
                            break;
                        }
                        if (smallHeight > height && height != 0)
                        {
                            smallHeight = height;
                        }
                        smallWidth = width;
                        break;
                    case ThumbnailType.Absolute:
                        smallHeight = height;
                        smallWidth = width;
                        break;
                }
                Bitmap bitmap = new Bitmap(Convert.ToInt32(smallWidth), Convert.ToInt32(smallHeight));
                bitmap.SetResolution(72, 72);
                System.Drawing.Graphics grap = Graphics.FromImage(bitmap);
                grap.DrawImage(original, 0, 0, smallWidth, smallHeight);
                bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                grap.Dispose();
                bitmap.Dispose();
                original.Dispose();
            }
            catch (Exception ex)
            {
                throw new Utility.LogHepler(ex);
            }


        }


    }
    /// <summary>
    /// 缩略所依据高度还是宽度
    /// </summary>
    /// ===============================================================
    /// 作者：邹学良 
    /// 时间：2013-03-21
    /// 描述：创建
    /// 历史：无
    /// ===============================================================
    public enum ThumbnailType
    {
        /// <summary>
        /// 根据高度缩略
        /// </summary>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        Height
        ,
        /// <summary>
        /// 根据宽度缩略
        /// </summary>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        Width
        ,
        /// <summary>
        /// 固定
        /// </summary>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        Absolute
    }

}